package com.tinyengine.it.controller.endpoint;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.entity.ApiGatewayService;
import com.tinyengine.it.model.entity.DbLogicalInfo;
import com.tinyengine.it.model.entity.DbTableInfo;
import com.tinyengine.it.model.vo.ApiGatewayServiceVo;
import com.tinyengine.it.service.db.DbLogicalInfoService;
import com.tinyengine.it.service.db.DbTableInfoService;
import com.tinyengine.it.service.endpoint.ApiGatewayServiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/gateway/service/api")
@Tag(name = "网关服务配置")
@RequiredArgsConstructor
public class ApiGatewayServiceController {

    private final ApiGatewayServiceService apiGatewayServiceService;
    private final DbTableInfoService dbTableInfoService;
    private final DbLogicalInfoService dbLogicalInfoService;


    @Operation(summary = "获取服务列表",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/list")
    public Result list() {
        List<ApiGatewayService> list = apiGatewayServiceService.list();
        return Result.success(list);
    }

    @Operation(summary = "新增及更新",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(@RequestBody @Valid ApiGatewayServiceVo serviceVo, @RequestHeader(required = false) Integer appId) {

        LambdaQueryWrapper<ApiGatewayService> lambdaQuery = Wrappers.lambdaQuery();

        if (ObjUtil.isNotNull(appId)) {
            lambdaQuery.eq(ApiGatewayService::getAppId, appId);
        }
        lambdaQuery.eq(ApiGatewayService::getServiceEn, serviceVo.getServiceEn()).last("limit 1");

        ApiGatewayService isExist = apiGatewayServiceService.getOne(lambdaQuery);
        if (ObjUtil.isNull(serviceVo.getId()) && ObjUtil.isNotNull(isExist)) {
            throw new RuntimeException("服务Code已经存在，请修改!");
        }
        ApiGatewayService service = BeanUtil.copyProperties(serviceVo, ApiGatewayService.class);
        service.setAppId(appId);
        apiGatewayServiceService.saveOrUpdate(service);
        //将所有数据包含新增后的Id返回
        return Result.success(service);
    }

    @Operation(summary = "根据ID删除字段",
            parameters = {
                    @Parameter(name = "id", description = "ID")},
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@Valid @PathVariable Integer id) {
        dbTableInfoService.remove(Wrappers.<DbTableInfo>lambdaQuery().eq(DbTableInfo::getServiceId, id));
        dbLogicalInfoService.remove(Wrappers.<DbLogicalInfo>lambdaQuery().eq(DbLogicalInfo::getServiceId, id));
        apiGatewayServiceService.removeById(id);
        return Result.success("该服务下的逻辑实体及实体已全部删除!!!");
    }

}
